﻿using AutoMapper;
using BaseAPI;
using MemorySiteServer.Models;
using MemorySiteServer.ViewModels.FavoriteGroupViewModels;
using Microsoft.AspNetCore.Mvc;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MemorySiteServer.Controllers
{
    /// <summary>
    /// 收藏夹目录
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class FavoriteGroupController : BaseController<FavoriteGroup>
    {
        public FavoriteGroupController(DataContext context, IMapper mapper, IdWorker idWorker) : base(context, mapper, idWorker) { }

        /// <summary>
        /// 添加目录
        /// </summary>
        /// <param name="favoriteGroupCreateRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseModel<string>> Create(FavoriteGroupCreateRequest favoriteGroupCreateRequest)
        {
            return await Z_Create(favoriteGroupCreateRequest);
        }

        /// <summary>
        /// 修改目录
        /// </summary>
        /// <param name="favoriteGroupPatchRequest"></param>
        /// <returns></returns>
        [HttpPatch]
        public async Task<ResponseModel<string>> Modify(FavoriteGroupPatchRequest favoriteGroupPatchRequest)
        {
            return await Z_Modify(favoriteGroupPatchRequest);
        }

        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResponseModel<string>> Delete(long id)
        {
            return await Z_Delete(id);
        }

        /// <summary>
        /// 获取目录列表
        /// </summary>
        /// <param name="favoriteGroupGetListRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseModel<List<FavoriteGroupGetListResponse>>> GetList(FavoriteGroupGetListRequest favoriteGroupGetListRequest)
        {
            var result = await Z_GetList(favoriteGroupGetListRequest);
            if (result.Code == 1)
            {
                var data = new List<FavoriteGroupGetListResponse>();
                var transFun = new Func<FavoriteGroup, FavoriteGroupGetListResponse>(x => new FavoriteGroupGetListResponse()
                {
                    ID = x.ID.ToString(),
                    Name = x.Name,
                    Favorites = x.Favorites.Select(y => new Favorite()
                    {
                        ID = y.ID,
                        Name = y.Name,
                        FavoriteGroupID = y.FavoriteGroupID,
                        Url = y.Url,
                        Remark = y.Remark
                    }).ToList(),
                    Children = new List<FavoriteGroupGetListResponse>()
                });
                //目录中只有一个根目录，由于数据库初始化，必定存在这样一个root
                var rootOriginal = result.Data.FirstOrDefault();
                result.Data.Remove(rootOriginal);
                var root = transFun(rootOriginal);
                //本地函数
                void setChildren(FavoriteGroupGetListResponse x)
                {
                    var childrenOriginal = result.Data.Where(y => y.ParentID.Value.ToString() == x.ID).ToList();
                    result.Data.RemoveAll(y => y.ParentID.Value.ToString() == x.ID);
                    x.Children.AddRange(childrenOriginal.Select(transFun).ToList());
                    x.Children.ForEach(y => setChildren(y));
                }
                setChildren(root);
                data.Add(root);
                return ResponseModel<List<FavoriteGroupGetListResponse>>.Success(data);
            }
            else
                return ResponseModel<List<FavoriteGroupGetListResponse>>.Failed(result.Message);
        }

    }
}
